﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
  public partial class WorkflowInstancesControl : AdminUserControl
  {
    public WorkflowInstancesControl()
    {
      InitializeComponent();

      UIActionList actions = new UIActionList();
      actions.Add("Refresh", "Refreshes the list of workflow instances",
        new ActionExecuteDelegate(RefreshWorkflowInstances));
      actions.Add("Delete selected", "Deletes the selected workflow instances",
        new ActionExecuteDelegate(DeleteWorkflowInstances));
      uiActionsControl.Actions = actions;
    }

    private WorkflowInstanceCollection workflowInstances;
    internal void LoadInstances(WorkflowInstanceCollection workflowInstances)
    {
      this.workflowInstances = workflowInstances;
      instancesGrid.DataSource = null;
      instancesGrid.DataSource = workflowInstances;
      instancesGrid.ClearColumns();
      instancesGrid.AddColumn("Guid");
      instancesGrid.AddColumn("ParentFolderId", "Parent");
      instancesGrid.AddColumn("Created");
      instancesGrid.AutosizeColumns();
    }

    private string RefreshWorkflowInstances()
    {
      workflowInstances.Refresh();
      LoadInstances(workflowInstances);
      OnRefreshRequired();
      return null;
    }

    private string DeleteWorkflowInstances()
    {
      if (MessageBox.Show("Delete the selected workflow instances?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          instancesGrid.DeleteSelected();
          OnRefreshRequired();
        }
      }
      return null;
    }
  }
}
